//
//  Forum_detail_ViewController.m
//  Pacific_Computer
//
//  Created by 范琦 on 14-12-17.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "Forum_detail_ViewController.h"
#import "AppDelegate.h"
#import "WeiboSDK.h"
#import "WBHttpRequest.h"

@interface Forum_detail_ViewController ()<UIWebViewDelegate,UIWebViewDelegate>

@property(nonatomic, retain) UIWebView *web;
@property(nonatomic, retain) UIView *loadview1;
@property(nonatomic, retain) UIActivityIndicatorView *act;
@property(nonatomic, retain) NSString *str;
@property(nonatomic, retain) NSString *stb;
@property(nonatomic, retain) NSString *strImage;

@end

@implementation Forum_detail_ViewController
- (void)dealloc
{
    [_web release];
    [_loadview1 release];
    [_act release];
    [_five release];
    [_four release];
    [_more release];
    [_list release];
    [_stb release];
    [_str release];
    [_str release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.five.title;
    
    self.web = [[UIWebView alloc]initWithFrame:CGRectMake(0, -self.view.bounds.size.height * 26 / 100, self.view.bounds.size.width, self.view.bounds.size.height * 1.309)];
    [self.view addSubview:self.web];
    [_web release];
    self.web.delegate = self;
    
    if (self.five != nil) {
        NSString *str = [self.five.topicUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:str] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20];
        [self.web loadRequest:request];
        self.str = self.five.title;
        self.stb = self.five.forumUrl;
        self.strImage = @"";
 
        
        
    }
    else if (self.four != nil){
        NSString *str = [NSString stringWithFormat:@"http://%@.pconline.com.cn/mobile/%@.html",self.four.from, self.four.topicId];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:str] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20];
        [self.web loadRequest:request];
        self.str = self.four.title;
        self.strImage = self.four.image;
        self.stb = str;
        NSLog(@"11111111 = %@",self.stb);
        
    }else if (self.list != nil){
        NSString *str = [self.list.uri stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:str] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20];
        [self.web loadRequest:request];
        self.str = self.list.title;
        self.strImage = self.list.image;
        self.stb = self.list.uri;
        
    }else if (self.more != nil){
        NSString *str = [self.more.topicUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:str] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20];
        [self.web loadRequest:request];
        self.str = self.more.title;
        self.strImage = @"";
        self.stb = self.more.forumName;

    }
    
    self.loadview1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    self.loadview1.backgroundColor = [UIColor blackColor];
    self.loadview1.alpha = 0.3;
    [self.view addSubview:self.loadview1];
    [_loadview1 release];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, self.view.bounds.size.width * 8 / 100, self.view.bounds.size.height * 4.49 / 100);
    [button setImage:[UIImage imageNamed:@"fengxiang2"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(fengxiangAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    
}

- (WBMessageObject *)messageToShare
{
    WBMessageObject *message = [WBMessageObject message];
    NSString *str = [NSString stringWithFormat:@"%@ #%@#:",self.stb,self.str];
    message.text = str;
    WBImageObject *image = [WBImageObject object];
    NSString *path = self.strImage;
    if ([self.strImage isEqualToString:@""]) {
        WBImageObject *image = [WBImageObject object];
        image.imageData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"meijing3" ofType:@"png"]];
        message.imageObject = image;
    }else{
        
        NSURL *url = [NSURL URLWithString:path];
        image.imageData = [NSData dataWithContentsOfURL:url];
        message.imageObject = image;
        
    }
    

    return message;
}

- (void)fengxiangAction:(UIButton *)button
{
    AppDelegate *myDelegate =(AppDelegate*)[[UIApplication sharedApplication] delegate];
    WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
    authRequest.redirectURI = @"https://api.weibo.com/oauth2/default.html";
    authRequest.scope = @"all";
    
    WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:[self messageToShare] authInfo:authRequest access_token:myDelegate.wbtoken];
    request.userInfo = @{@"ShareMessageFrom": @"SendMessageToWeiboViewController",
                         @"Other_Info_1": [NSNumber numberWithInt:123],
                         @"Other_Info_2": @[@"obj1", @"obj2"],
                         @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
    //    request.shouldOpenWeiboAppInstallPageIfNotInstalled = NO;
    [WeiboSDK sendRequest:request];
}




- (void)webViewDidStartLoad:(UIWebView *)webView
{
    self.act = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    self.act.frame = CGRectMake(0, 0, 32, 32);
    self.act.center = self.web.center;
    [self.loadview1 addSubview:self.act];
    [self.act startAnimating];
    [_act release];


}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.act stopAnimating];
    [self.loadview1 removeFromSuperview];


}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    
//    NSLog(@"加载失败");
//    NSLog(@"failLoadWithError: %@", error);
    [self.act stopAnimating];
    [self.loadview1 removeFromSuperview];
    if([error code] == NSURLErrorCancelled)
    {
        return;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
